package com.demo.service;

import java.io.File;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.demo.common.model.Upload;
import com.demo.utils.DateU;
import com.jfinal.upload.UploadFile;

public class UploadService {
	
	public final static UploadService serivce = new UploadService();
	
	//上传图片主方法
	public List<Map<String, Object>> upload(String path,String channel,List<UploadFile> files){
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>(); 
		for(UploadFile file:files){
			String uploadPath = file.getUploadPath();
			String OriginalFileName = file.getOriginalFileName();
			String contentType = file.getContentType();
			String[] suffix = OriginalFileName.split("\\.");
			String randName = DateU.formatDate(new Date(), "yyyyMMddHHmmss")+"."+suffix[1];
			File f = file.getFile();
			Upload upload = new Upload();
			upload.set("channel", channel);
			upload.set("fileType", contentType);
			upload.set("uploadDate", new Date());
			upload.set("fileSrcName", OriginalFileName);
			upload.set("fileLinkAttr", uploadPath);
			upload.set("fileRandName", randName);
			upload.set("fileSize", (new DecimalFormat(".00").format(f.length()/1024.00))+"kb");
			upload.save();
			
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("id", upload.getId());
			map.put("fileName", randName);
			map.put("path", uploadPath);
			list.add(map);
		}
		return list; 
		
		
	}

}
